#!/bin/bash

# FTP文件传输测试脚本
echo "🧪 FTP文件传输测试脚本"
echo "======================="

# 设置颜色
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color

# 函数：打印成功消息
print_success() {
    echo -e "${GREEN}✅ $1${NC}"
}

# 函数：打印错误消息
print_error() {
    echo -e "${RED}❌ $1${NC}"
}

# 函数：打印警告消息
print_warning() {
    echo -e "${YELLOW}⚠️ $1${NC}"
}

# 函数：打印信息消息
print_info() {
    echo -e "${YELLOW}ℹ️ $1${NC}"
}

# 检查Rust是否安装
check_rust() {
    print_info "检查Rust环境..."
    if command -v cargo &> /dev/null; then
        print_success "Rust环境正常"
        cargo --version
    else
        print_error "Rust未安装，请先安装Rust"
        exit 1
    fi
}

# 构建客户端
build_client() {
    print_info "构建FTP客户端..."
    if cargo build -p ftp-client; then
        print_success "客户端构建成功"
    else
        print_error "客户端构建失败"
        exit 1
    fi
}

# 创建测试文件
create_test_files() {
    print_info "创建测试文件..."
    
    # 创建小文件
    echo "Hello, FTP World!" > test_small.txt
    print_success "创建小测试文件: test_small.txt"
    
    # 创建中等文件
    for i in {1..100}; do
        echo "Line $i: This is a test line for FTP file transfer." >> test_medium.txt
    done
    print_success "创建中等测试文件: test_medium.txt"
    
    # 创建大文件（1MB）
    dd if=/dev/zero of=test_large.bin bs=1024 count=1024 2>/dev/null
    print_success "创建大测试文件: test_large.bin (1MB)"
}

# 测试文件上传
test_file_upload() {
    print_info "测试文件上传..."
    
    # 测试小文件上传
    print_info "上传小文件..."
    if cargo run -p ftp-client -- --host 127.0.0.1 --port 21 --username testuser --password testpass --upload test_small.txt --local_path test_small.txt 2>&1 | grep -q "Upload completed"; then
        print_success "小文件上传成功"
    else
        print_warning "小文件上传失败（可能需要先启动服务器）"
    fi
    
    # 测试中等文件上传
    print_info "上传中等文件..."
    if cargo run -p ftp-client -- --host 127.0.0.1 --port 21 --username testuser --password testpass --upload test_medium.txt --local_path test_medium.txt 2>&1 | grep -q "Upload completed"; then
        print_success "中等文件上传成功"
    else
        print_warning "中等文件上传失败"
    fi
    
    # 测试大文件上传
    print_info "上传大文件..."
    if cargo run -p ftp-client -- --host 127.0.0.1 --port 21 --username testuser --password testpass --upload test_large.bin --local_path test_large.bin 2>&1 | grep -q "Upload completed"; then
        print_success "大文件上传成功"
    else
        print_warning "大文件上传失败"
    fi
}

# 测试文件下载
test_file_download() {
    print_info "测试文件下载..."
    
    # 测试小文件下载
    print_info "下载小文件..."
    if cargo run -p ftp-client -- --host 127.0.0.1 --port 21 --username testuser --password testpass --download test_small.txt --local_path downloaded_small.txt 2>&1 | grep -q "Download completed"; then
        print_success "小文件下载成功"
        # 验证文件内容
        if diff test_small.txt downloaded_small.txt > /dev/null; then
            print_success "小文件内容验证通过"
        else
            print_warning "小文件内容验证失败"
        fi
    else
        print_warning "小文件下载失败"
    fi
    
    # 测试中等文件下载
    print_info "下载中等文件..."
    if cargo run -p ftp-client -- --host 127.0.0.1 --port 21 --username testuser --password testpass --download test_medium.txt --local_path downloaded_medium.txt 2>&1 | grep -q "Download completed"; then
        print_success "中等文件下载成功"
        # 验证文件内容
        if diff test_medium.txt downloaded_medium.txt > /dev/null; then
            print_success "中等文件内容验证通过"
        else
            print_warning "中等文件内容验证失败"
        fi
    else
        print_warning "中等文件下载失败"
    fi
    
    # 测试大文件下载
    print_info "下载大文件..."
    if cargo run -p ftp-client -- --host 127.0.0.1 --port 21 --username testuser --password testpass --download test_large.bin --local_path downloaded_large.bin 2>&1 | grep -q "Download completed"; then
        print_success "大文件下载成功"
        # 验证文件内容
        if diff test_large.bin downloaded_large.bin > /dev/null; then
            print_success "大文件内容验证通过"
        else
            print_warning "大文件内容验证失败"
        fi
    else
        print_warning "大文件下载失败"
    fi
}

# 测试传输性能
test_transfer_performance() {
    print_info "测试传输性能..."
    
    # 创建性能测试文件
    dd if=/dev/urandom of=perf_test.bin bs=1024 count=10240 2>/dev/null  # 10MB文件
    print_success "创建性能测试文件: perf_test.bin (10MB)"
    
    # 测试上传性能
    print_info "测试上传性能..."
    start_time=$(date +%s)
    if cargo run -p ftp-client -- --host 127.0.0.1 --port 21 --username testuser --password testpass --upload perf_test.bin --local_path perf_test.bin 2>&1 | grep -q "Upload completed"; then
        end_time=$(date +%s)
        duration=$((end_time - start_time))
        print_success "上传性能测试完成，耗时: ${duration}秒"
    else
        print_warning "上传性能测试失败"
    fi
    
    # 测试下载性能
    print_info "测试下载性能..."
    start_time=$(date +%s)
    if cargo run -p ftp-client -- --host 127.0.0.1 --port 21 --username testuser --password testpass --download perf_test.bin --local_path downloaded_perf_test.bin 2>&1 | grep -q "Download completed"; then
        end_time=$(date +%s)
        duration=$((end_time - start_time))
        print_success "下载性能测试完成，耗时: ${duration}秒"
    else
        print_warning "下载性能测试失败"
    fi
}

# 清理测试文件
cleanup_test_files() {
    print_info "清理测试文件..."
    rm -f test_small.txt test_medium.txt test_large.bin
    rm -f downloaded_small.txt downloaded_medium.txt downloaded_large.bin
    rm -f perf_test.bin downloaded_perf_test.bin
    print_success "测试文件已清理"
}

# 运行完整测试
run_full_test() {
    print_info "开始FTP文件传输完整测试"
    
    check_rust
    build_client
    create_test_files
    test_file_upload
    test_file_download
    test_transfer_performance
    cleanup_test_files
    
    print_success "所有文件传输测试完成！"
}

# 显示帮助信息
show_help() {
    echo "用法: $0 [选项]"
    echo ""
    echo "选项:"
    echo "  build      - 构建客户端"
    echo "  upload     - 测试文件上传"
    echo "  download   - 测试文件下载"
    echo "  performance - 测试传输性能"
    echo "  test       - 运行所有测试"
    echo "  cleanup    - 清理测试文件"
    echo "  help       - 显示此帮助信息"
    echo ""
    echo "示例:"
    echo "  $0 build      # 构建客户端"
    echo "  $0 test       # 运行所有测试"
    echo "  $0 upload     # 只测试上传"
}

# 主函数
main() {
    case "${1:-test}" in
        "build")
            check_rust
            build_client
            ;;
        "upload")
            check_rust
            test_file_upload
            ;;
        "download")
            check_rust
            test_file_download
            ;;
        "performance")
            check_rust
            test_transfer_performance
            ;;
        "test")
            run_full_test
            ;;
        "cleanup")
            cleanup_test_files
            ;;
        "help"|"-h"|"--help")
            show_help
            ;;
        *)
            print_error "未知选项: $1"
            show_help
            exit 1
            ;;
    esac
}

# 运行主函数
main "$@"
